##Renewable Energy Dataset

library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
energy<-read_csv('../../data/IRENA data.csv', skip=1)
## Rows: 67200 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Country/area, Technology, Data Type, Grid connection, Electricity s...
## dbl (1): Year
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(energy)

energy_2<-energy%>%
  rename(country='Country/area')

high_gdp<-c("United States","Qatar","Norway","Singapore","United Arab Emirates",
            "Switzerland","Ireland","Luxembourg","Saudi Arabia","Taiwan")
low_gdp<-c("Burundi","Central Africa Republic","Liberia","Democratic Republic of Congo",
           "Mozambique","Niger","Madagascar","Malwai","Chad","Afghanistan")
energy_2$`Electricity statistics`<-
  as.numeric(gsub("-",NA,energy_2$`Electricity statistics`))
world<-energy_2%>%
  group_by(Technology,`Data Type`,`Grid connection`,Year)%>%
  summarise(`Electricity statistics`=sum(`Electricity statistics`,na.rm=TRUE),
            .groups="drop")%>%
  mutate('country'="World")
energy_2<-bind_rows(energy_2,world)

energy_groups<-energy_2%>%
  mutate(group=case_when(
    country=="World"~"World",
    country%in%high_gdp~"High GDP",
    country %in%low_gdp~"Low GDP",
    TRUE~NA_character_))%>%
  filter(!is.na(group))
energy_groups_sdg<-energy_groups%>%
  filter(Year%in% c(2012,2023))
energy_groups_sdg<-energy_groups_sdg%>%
  group_by(group,Year,Technology)%>%
  summarise(total=sum(`Electricity statistics`, na.rm=TRUE),
            .groups="drop")
energy_groups_no_total<-energy_groups_sdg%>%
  filter(!Technology%in%c("Total renewable","Total non-renewable","Total"))

ggplot(energy_groups_no_total,
       aes(x=group,
           y=total,
           fill=Technology))+
  geom_col(position="fill")+
  scale_y_continuous(labels=scales::percent)+
  facet_wrap(~Year)+
  coord_flip()

Gapminder Dataset

library(gapminder)
library(dplyr)
library(ggplot2)
data(gapminder)
gapminder%>%head
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.
gap<-gapminder%>%
  filter(country %in% c('United States', 'China', 'France','Liberia','Ethiopia','Haiti'))%>%
  filter(year>1970)
ggplot(gap,
       aes(x=year, 
           y=lifeExp,
           color=country,
           linetype=country))+
  geom_line(size=1, alpha=0.5)+
  scale_linetype_manual(values=c("China"="solid","France"="solid","United States"="solid",
                        "Ethiopia"="dashed","Haiti"="dashed","Liberia"="dashed")) +
  labs(title = "Life Expectancy for Developed and Undeveloped Countries Over Time",
  x = "Year",
  y = "Life Expectancy")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

View(gapminder)
gapminder%>%
  filter(year == 2007)%>%
   slice_min(order_by = gdpPercap, n=5)
## # A tibble: 5 × 6
##   country          continent  year lifeExp      pop gdpPercap
##   <fct>            <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Congo, Dem. Rep. Africa     2007    46.5 64606759      278.
## 2 Liberia          Africa     2007    45.7  3193942      415.
## 3 Burundi          Africa     2007    49.6  8390505      430.
## 4 Zimbabwe         Africa     2007    43.5 12311143      470.
## 5 Guinea-Bissau    Africa     2007    46.4  1472041      579.

##Carbon emmissions

library(dplyr)
library(readr)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggthemes)
url <-'https://nyc3.digitaloceanspaces.com/owid-public/data/co2/owid-co2-data.csv'
carbon <-
  read_csv(url) 
## Rows: 50191 Columns: 79
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): country, iso_code
## dbl (77): year, population, gdp, cement_co2, cement_co2_per_capita, co2, co2...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(carbon)
carbon<-carbon%>%
  mutate(gdp_per_capita=gdp/population)
high_gdp<-c("United States","Qatar","Norway","Singapore","United Arab Emirates",
            "Switzerland","Ireland","Luxembourg","Saudi Arabia","Taiwan")
low_gdp<-c("Burundi","Central Africa Republic","Liberia","Democratic Republic of Congo",
           "Mozambique","Niger","Madagascar","Malwai","Chad","Afghanistan")
carbon_groups<-carbon%>%
  mutate(group=case_when(
    country=="World"~"World",
    country%in%high_gdp~"High GDP",
    country %in%low_gdp~"Low GDP",
    TRUE~NA_character_))%>%
  filter(!is.na(group))

carbon_wo_world<-carbon%>%
  filter(country!="World")
carbon_groups<-carbon_wo_world%>%
  mutate(group=case_when(
    country%in%high_gdp~"High GDP",
    country%in%low_gdp~"Low GDP",
    TRUE~"World"))
carbon_trend<-carbon_groups%>%
  group_by(year,group)%>%
  summarise(mean_temp=mean(temperature_change_from_ghg, na.rm=TRUE),
            .groups="drop")

ggplot(carbon_trend,
       aes(x=year,y=mean_temp,color=group))+
  geom_line(size=1)+
  geom_vline(xintercept=2012,
             linetype ="dashed",
             size=0.5)+
    scale_x_continuous(limits=c(1850,max(carbon_trend$year)))+
  labs(title="Average Temperature Change from Greenhouse Gas Emmissions",
       x="Year",
       y="Temperature Change",
       color="Key")
## Warning: Removed 303 rows containing missing values or values outside the scale range
## (`geom_line()`).

 #TEMP CHANGE FROM GHGS
library(dplyr)
library(readr)
library(ggplot2)
library(plotly)
library(ggthemes)
url <-'https://nyc3.digitaloceanspaces.com/owid-public/data/co2/owid-co2-data.csv'
carbon <-
  read_csv(url)
## Rows: 50191 Columns: 79
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): country, iso_code
## dbl (77): year, population, gdp, cement_co2, cement_co2_per_capita, co2, co2...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
carbon_groups<-carbon%>%
  mutate(group=case_when(
    country=="World"~"World",
    country%in%high_gdp~"High GDP",
    country %in%low_gdp~"Low GDP",
    TRUE~NA_character_))%>%
  filter(!is.na(group))

high_gdp<-c("United States","Qatar","Norway","Singapore","United Arab Emirates",
            "Switzerland","Ireland","Luxembourg","Saudi Arabia","Taiwan")
low_gdp<-c("Burundi","Central Africa Republic","Liberia","Democratic Republic of Congo",
           "Mozambique","Niger","Madagascar","Malwai","Chad","Afghanistan")
carbon_wo_world<-carbon%>%
  filter(country!="World")

carbon_groups<-carbon_wo_world%>%
  mutate(group=case_when(
    country%in%high_gdp~"High GDP",
    country%in%low_gdp~"Low GDP",
    TRUE~"World"))
carbon_trend<-carbon_groups%>%
  group_by(year,group)%>%
  summarise(mean_temp=mean(temperature_change_from_ghg, na.rm=TRUE),
            .groups="drop")

ggplot(carbon_trend,
       aes(x=year,y=mean_temp,color=group))+
  geom_line(size=1)+
  geom_vline(xintercept=2012,
             linetype ="dashed",
             size=0.5)+
    scale_x_continuous(limits=c(2000,max(carbon_trend$year)))+
  labs(title="Average Temperature Change from Greenhouse Gas Emmissions",
       x="Year",
       y="Temperature Change",
       color="Key")
## Warning: Removed 750 rows containing missing values or values outside the scale range
## (`geom_line()`).

##Levelized cost of energy

library(readr)
url <- 'https://raw.githubusercontent.com/ericmkeen/sewanee_esus/master/02_energy_sector/levelized-cost-of-energy.csv'
econ <- read_csv(url)
## Rows: 3402 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): country, source
## dbl (2): year, cost
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
econ%>%head
## # A tibble: 6 × 4
##   country    year source                     cost
##   <chr>     <dbl> <chr>                     <dbl>
## 1 Australia  2010 Bioenergy                NA    
## 2 Australia  2010 Geothermal               NA    
## 3 Australia  2010 Offshore wind            NA    
## 4 Australia  2010 Solar photovoltaic        0.424
## 5 Australia  2010 Concentrated solar power NA    
## 6 Australia  2010 Hydropower               NA
library(ggplot2)
library(dplyr)
library(readr)
url <- 'https://raw.githubusercontent.com/ericmkeen/sewanee_esus/master/02_energy_sector/levelized-cost-of-energy.csv'
econ <- read_csv(url)
## Rows: 3402 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): country, source
## dbl (2): year, cost
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(econ)
#cost of renewable and non-renewable sources
econ_filtered<-econ%>%
  filter(country%in% c("United States","France","Sweden","Germany","Japan","China","South Korea","United Kingdom","Netherlands","Denmark"))
            
p<-ggplot(econ_filtered,
       aes(x=year,
           y=cost,
           color=factor(source),
           text=paste("country:",country,
                      "<br>year:",year,
                      "<br>source:",source,
                      "<br>cost:",round(cost,2))))+
  geom_point()+
  geom_vline(xintercept=2012)
ggplotly(p,tooltip="text")
  annotate(geom='text',
           x=1990,y=0.1,
           size=3,
           label='Range of fissil fuel costs')#n breaks the line for captions on the graph if too long
## mapping: x = ~x, y = ~y 
## geom_text: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
library(dplyr)
library(knitr)
econ%>%
  group_by(country)%>%
  summarize(cost=mean(cost, na.rm=TRUE))%>%
  kable
country cost
Australia 0.1590544
Brazil 0.0797457
Canada 0.0985404
China 0.1030578
Denmark 0.1360360
France 0.1406183
Germany 0.1513690
India 0.1347036
Italy 0.1423896
Japan 0.1693647
Mexico 0.0660023
Netherlands 0.1032591
South Korea 0.1839150
Spain 0.1086002
Sweden 0.1391563
Turkey 0.0825361
Ukraine 0.2146782
United Kingdom 0.1494249
United States 0.1275501
Vietnam 0.1231400
World 0.1296533